package com.game77.guessTheWords2;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.adcocoa.sdk.AdcocoaPopupAd;
import com.game77.guessTheWords2.core.GameSystem;
import com.game77.guessTheWords2.scene.LoadingScene;
import com.game77.guessTheWords2.util.AppControl;
import com.game77.guessTheWords2.util.DianJinTool;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Director.IDirectorLifecycleListener;
import com.wiyun.engine.opengl.WYGLSurfaceView;

public class MainActivity extends Activity implements
		IDirectorLifecycleListener {
	static {
		System.loadLibrary("wiskia");
		System.loadLibrary("xml2");
		System.loadLibrary("wiengine");
		System.loadLibrary("wiengine_binding");
		System.loadLibrary("wisound");
	}

	// private
	WYGLSurfaceView mSurfaceView = null;
	private boolean mStarted = false;

	private boolean isLoaded = false;

	// public static
	public static MainActivity instance = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		instance = this;
		GameSystem.AndroidContext = this;
		// 设置窗口状态
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 竖屏

		// 处理低分辨率机型
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		if (dm.widthPixels < 480) {
			GameSystem.isLowPxPhone = true;
		}

		if (!isLoaded) {
			MobclickAgent.onError(this);
			mSurfaceView = new WYGLSurfaceView(this);
			mSurfaceView.setKeepScreenOn(true);
			setContentView(mSurfaceView);
			// 设置显示帧率，程序发布时应该去掉
			// Director.getInstance().setDisplayFPS(true);

			// 缩放模式
			Director.getInstance().setScaleMode(
					Director.SCALE_MODE_BASE_SIZE_FIT_XY);
			Director.getInstance().setBaseSize(GameSystem.Screen_Width,
					GameSystem.Screen_Height);

			Director.getInstance().addLifecycleListener(this);
			DianJinTool.init();
			AppControl.init();
			InitAdcocoa();
			isLoaded = true;
		}
	}

	private void InitAdcocoa() {
		// 代码设置平台ID
		AdcocoaPopupAd.setPlatformId("fdb9efd117c04e43d969de40cc7508fb");
		// 这里需要先调用平台的init方法！推荐写在onCreate的第一句。
		AdcocoaPopupAd.init(this);

	}

	public void UpdateVersion() {
		// 检测更新
		this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				UmengUpdateAgent.setUpdateOnlyWifi(false);
				UmengUpdateAgent.update(MainActivity.instance);
			}
		});
	}

	@Override
	protected void onStop() {
		super.onStop();
		Director.getInstance().pause();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		Director.getInstance().resume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onDestroy() {
		Director.getInstance().end();
		DianJinTool.destroy();
		super.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	@Override
	public void onDirectorEnded() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDirectorPaused() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDirectorResumed() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDirectorScreenCaptured(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSurfaceChanged(int w, int h) {
		if (!mStarted) {
			mStarted = true;
			Director.getInstance().runWithScene(LoadingScene.make());
		}

	}

	@Override
	public void onSurfaceCreated() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSurfaceDestroyed() {
		// TODO Auto-generated method stub

	}

	public void showTip(final String tip) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(MainActivity.instance, tip, Toast.LENGTH_SHORT)
						.show();
			}
		});
	}

	public void showExitTips() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				AlertDialog alertDialog = new AlertDialog.Builder(
						MainActivity.this)
						.setTitle("退出")
						.setMessage("是否要退出疯狂填字豪华版")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										MainActivity.this.finish();
									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										return;
									}
								}).setCancelable(false).create(); // 创建对话框
				alertDialog.show();
			}
		});
	}
}